<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      v-model:current-page="currentPage"
      :pager-count="pagerCount"
      v-model:page-size="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="Number(total)"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
const emit = defineEmits(['update:page', 'update:limit', 'pagination'])
const props = defineProps({
  total: {
    required: true,
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 20
  },
  page: {
    type: Number,
    default: 1
  },
  pageSizes: {
    type: Array,
    default() {
      // return [5, 10, 15, 20, 30, 50]
      return [10, 15, 20, 30, 50]
    }
  },
  pagerCount: {
    type: [String, Number],
    default: 7
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  },
  scrollTop: {
    type: Boolean,
    default: true
  }
})

const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  }
})
const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  }
})

const handleSizeChange = val => {
  console.log('当前页', currentPage.value)
  console.log('页码', val)
  emit('pagination', { page: currentPage.value, limit: val })
  let currentCpn = getCurrentInstance()

  // console.log(currentCpn.parent);
  // scrollTop &&
  //   (currentCpn.parent.$refs.pubTable.$refs.table.bodyWrapper.scrollTop = 0)
}

const handleCurrentChange = val => {
  console.log('当前页2', pageSize.value)

  emit('pagination', { page: val, limit: pageSize.value })
  // let currentCpn = getCurrentInstance()
  // scrollTop &&
  //   (currentCpn.parent.$refs.pubTable.$refs.table.bodyWrapper.scrollTop = 0)
}
</script>
